package rcs34.android.Namedays;

import java.util.HashMap;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;

public class GApp extends Application {
  public static final String EXTRA_WID = "wid";
  //public static final String ACTION_DIE = "die";
  public static final String KEY_POSITION = "pos";
  public static final String KEY_LASTRUNAS = "lra";
  public static final int V_MOREWIDGETS = 7;
  public static Boolean isPro;
  public static Context context;
  public static int position;
  public static int lastRunAs;
  private static SharedPreferences gPrefs;
  public static String packageName;
  public static Resources resources;
  public static boolean isALeepYear = false;
  public static String tableCZ;
  public static String tableDE;
  public static String tableES;
  public static String tableFR;
  public static String tableHU;
  public static String tableIE;
  public static String tableIT;
  public static String tablePL;
  public static String tableRU;
  public static String tableSK;
  public static String tableSV;
  public static String tableUK;
  public static boolean dDisplayDate;
  public static String dFontSize;
  public static String dcADayFont;
  public static String dcADayTop;
  public static String dcADayBottom;
  public static String dcNDayFont;
  public static String dcNDayTop;
  public static String dcNDayBottom;
  public static HashMap<String, Integer> flags = new HashMap<String, Integer>();
  public static boolean mmddDate;
  public void onCreate() {
    super.onCreate();
    GApp.context = getApplicationContext();
    packageName = this.getPackageName();
    resources = getResources();
    isPro = Boolean.parseBoolean(getString(R.string.pro_version));
    tableCZ = getString(R.string.tabCZ);
    tableDE = getString(R.string.tabDE);
    tableES = getString(R.string.tabES);
    tableFR = getString(R.string.tabFR);
    tableHU = getString(R.string.tabHU);
    tableIE = getString(R.string.tabIE);
    tableIT = getString(R.string.tabIT);
    tablePL = getString(R.string.tabPL);
    tableRU = getString(R.string.tabRU);
    tableSK = getString(R.string.tabSK);
    tableSV = getString(R.string.tabSV);
    tableUK = getString(R.string.tabUK);
    flags.put(tableCZ, R.raw.cscz_flag);
    flags.put(tableDE, R.raw.dede_flag);
    flags.put(tableES, R.raw.eses_flag);
    flags.put(tableFR, R.raw.frfr_flag);
    flags.put(tableHU, R.raw.huhu_flag);
    flags.put(tableIE, R.raw.enie_flag);
    flags.put(tableIT, R.raw.itit_flag);
    flags.put(tablePL, R.raw.plpl_flag);
    flags.put(tableRU, R.raw.ruru_flag);
    flags.put(tableSK, R.raw.sksk_flag);
    flags.put(tableSV, R.raw.svse_flag);
    flags.put(tableUK, R.raw.ukua_flag);
    gPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    position = gPrefs.getInt(KEY_POSITION, 0);
    lastRunAs = gPrefs.getInt(KEY_LASTRUNAS, 0);
    // We can safely remove the following code after everyone has the version
    // code 7 ;-)
    dDisplayDate = gPrefs.getBoolean(getString(R.string.prefDisplayDate),
        Boolean.parseBoolean(getString(R.string.dDisplayDate)));
    dFontSize = gPrefs.getString(getString(R.string.prefFontSize), "3");
    dcADayFont = gPrefs.getString(getString(R.string.prefADayColorFont),
        getString(R.string.dcADayFont));
    dcADayTop = gPrefs.getString(getString(R.string.prefADayColorTop),
        getString(R.string.dcADayTop));
    dcADayBottom = gPrefs.getString(getString(R.string.prefADayColorBottom),
        getString(R.string.dcADayBottom));
    dcNDayFont = gPrefs.getString(getString(R.string.prefNDayColorFont),
        getString(R.string.dcNDayFont));
    dcNDayTop = gPrefs.getString(getString(R.string.prefNDayColorTop),
        getString(R.string.dcNDayTop));
    dcNDayBottom = gPrefs.getString(getString(R.string.prefNDayColorBottom),
        getString(R.string.dcNDayBottom));
    mmddDate=getString(R.string.dateFormat).equals("M.d.");
  }
  
  public static SharedPreferences getWidgetPreferences(int _id) {
    if (isPro) {
      return context.getSharedPreferences(getWidgetPrefix(_id),
          Context.MODE_PRIVATE);
    } else {
      return gPrefs;
    }
  }
  
  public static void setPosition(int _position){
  	position = _position;
    SharedPreferences gPrefs = PreferenceManager
        .getDefaultSharedPreferences(GApp.context);
    gPrefs.edit().putInt(GApp.KEY_POSITION, position);
  }

  
  public static String getWidgetPrefix(int _id) {
    return "w" + _id + "_";
  }
}
